package com.example.shangjinlieren.fractionPage;

import com.example.shangjinlieren.ResourceTable;
import com.example.shangjinlieren.compontents.ImageNetWork;
import com.example.shangjinlieren.database.DbKv;
import com.example.shangjinlieren.slice.MainMoneyListDetailSlice;
import com.example.shangjinlieren.tabbar.fraction.BaseFraction;
import com.example.shangjinlieren.utils.RpxUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

public class MoneyListhistordemo extends BaseFraction {

    private   Context context;
    private AbilitySlice abilitySlice;
     private DbKv dbKv;
    private DirectionalLayout componentById;
    public MoneyListhistordemo(AbilitySlice abilitySlice) {
        this.abilitySlice = abilitySlice;

    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {

        this.context = container.getContext();
        this.dbKv = new DbKv(container.getContext());
        Component MoneyListBody =scatter.parse(ResourceTable.Layout_money_list
                , null, false);
//         this.componentById =(DirectionalLayout) MoneyListBody.findComponentById(ResourceTable.Id_moneylistbody);
        demo( scatter,  container,  intent);
        return MoneyListBody;
    }


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

    }
    protected void initPage(){
        
    }
    protected void initBanner(){
        PageSlider pageSlider = new PageSlider(this.context);
        pageSlider.setHeight((int) RpxUtil.rpx(300,this.context));
        pageSlider.setWidth((int) RpxUtil.rpx(750,this.context));
        Image image = new Image(this.context);
        image.setPixelMap(ResourceTable.Media_icon);
        pageSlider.addComponent(image);
        PageFlipper pageFlipper = new PageFlipper(this.context);
    }
    public void demo(LayoutScatter scatter, ComponentContainer container, Intent intent){
                    Button button = new Button(this.context);
                    Button button2 = new Button(this.context);
                    Button button1 = new Button(this.context);
                      Button buttonhttptext = new Button(this.context);
                     Button buttonhttp = new Button(this.context);
                    Button buttontext = new Button(this.context);
                    button.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
                    button.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
                    ShapeElement shapeElement = new ShapeElement();
                    RgbColor rgbColor = new RgbColor(1, 255, 255);
                    shapeElement.setRgbColor(rgbColor);
                    shapeElement.setCornerRadius(RpxUtil.rpx(23,this.context));
                    button.setPadding((int) RpxUtil.rpx(17,this.context),
                            (int) RpxUtil.rpx(11,this.context),
                            (int) RpxUtil.rpx(17,this.context),
                            (int) RpxUtil.rpx(11,this.context)
                    );

                    button.setBackground(shapeElement);
                    button.setText("跳转详情页");
                    button2.setText("存数据k1");
                    button2.setTextSize((int) RpxUtil.rpx(24,this.context));
                    Image image1 = new Image(this.context);
                    button2.setMarginTop((int) RpxUtil.rpx(24,this.context));
                    button1.setMarginTop((int) RpxUtil.rpx(24,this.context));
                    buttontext.setTextSize((int) RpxUtil.rpx(24,this.context));
                    button1.setTextSize((int) RpxUtil.rpx(24,this.context));
        buttonhttp.setTextSize((int) RpxUtil.rpx(24,this.context));
        buttonhttp.setMarginTop((int) RpxUtil.rpx(24,this.context));
        buttonhttptext.setTextSize((int) RpxUtil.rpx(24,this.context));
                    button1.setText("显示存的数据");
                    buttonhttp.setText("访问网络");
                    buttonhttp.setClickedListener(new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
//                            HmOSImageLoader hmOSImageLoader = new HmOSImageLoader(abilitySlice)
//                                    .load("http://image.51what.cn/public/upload/20190614/7036b8f43c1deba54d3eb4741efd66c1.png");
                        new ImageNetWork(abilitySlice,image1,"http://image.51what.cn/public/upload/20190614/7036b8f43c1deba54d3eb4741efd66c1.png").start();

                        }
                    });

        button2.setClickedListener(new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
                            System.out.println("我存数据了");
                            dbKv.getPreferences().putString("k1","我是k1");
                            dbKv.getPreferences().flush();
                        }
                    });
                    button1.setClickedListener(new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
                            System.out.println("dayin");
                            String string =  dbKv.getPreferences().getString("k1", "没获取到值");
                            buttontext.setText(string);
                        }
                    });
            //        dbKv

                    button.setClickedListener(listener->{
                        //目的
                        abilitySlice.present(new MainMoneyListDetailSlice(),intent);
            //        Operation o =  new Intent.OperationBuilder()
            //                    .withDeviceId("")
            //                  .withBundleName("com.example.shangjinlieren")
            //                .withAbilityName("com.example.shangjinlieren.MoneyListDetail")
            //                  .build();
            //        intent.setOperation(o);
            //        getFractionAbility().startAbility(intent);

                    });
                    button.setTextSize((int) RpxUtil.rpx(24,this.context));

                    componentById.setAlignment(LayoutAlignment.CENTER);
                    componentById.addComponent(button);
                    componentById.addComponent(button2);
                    componentById.addComponent(button1);
                    componentById.addComponent(buttontext);
                    componentById.addComponent(buttonhttp);
                    componentById.addComponent(image1);
                    componentById.addComponent(buttonhttptext);
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onForeground(Intent intent) {
        System.out.println("MoneyList+onForeground");
        super.onForeground(intent);
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public int getUIContent() {
        return ResourceTable.Layout_money_list;
    }

    @Override
    public void initComponent() {

    }
}
